<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
  <script type="text/javascript" src="/js/core.js"></script>
  <script type="text/javascript" src="/js/layzr.min.js"></script>
  <link rel="stylesheet" type="text/css" href="css/core.css">
  <title>IPA Server</title>
  <style type="text/css">
    * {
      padding: 0;
      margin: 0;
      font-size: 1em;
      font-family: sans-serif;
    }
    #info {
      display: flex;
      flex-direction: column;
      align-items: center;
      color: #A9B1B3;
      font-size: 1em;
      padding: 4em 0 0 0;
      font-weight: 100;
    }
    #info .name {
      color: #505556;
      font-size: 1.6em;
      font-weight: 500;
      margin: 1em 0;
    }
    #info img {
      width: 7.5em;
      height: 7.5em;
      border-radius: 20%;
      box-shadow: 0 0 4px #eee;
    }
    #info .install {
      background: #1890ff;
      border-radius: 5em;
      padding: 10px 5em;
      font-size: 1em;
      font-weight: 100;
      color: #fff;
      margin: 2em 0;
    }
  </style>
</head>
<body>
  <div id="info"></div>
  <div id="list"></div>
  <script type="text/javascript">

    // init lazy load
    const instance = Layzr()

    function query() {
      const obj = {}
      window.location.search.split('?')[1].split('&').map(row => {
        const v = row.split('=')
        if (v.length === 2) { obj[v[0]] = v[1] }
      })
      return obj
    }
    function loadInfo() {

      IPA.fetch(`/api/info/${query().id}`).then(row => {
        document.querySelector('#info').innerHTML = `
          <img src="${row.webIcon}" alt="">
          <div class="name">${row.name}</div>
          <div>${IPA.langString('Beta')} - ${row.version}(Build ${row.build}) - ${IPA.sizeStr(row.size)}</div>
          <div class='date'>${IPA.langString('Upload Date: ')}${row.date}</div>
          <div onclick="onClickInstall('${row.plist}')" class="install">${IPA.langString('Download and Install')}</div>
        `
        document.querySelector('#list').innerHTML = row.history.map(row => IPA.createItem(row)).join('')

        // start lazy load
        instance.update().check().handlers(true)
      })
    }
    function onClickInstall(plist) {
      window.location.href = 'itms-services://?action=download-manifest&url=' + plist
    }
    window.addEventListener('load', loadInfo)
  </script>
</body>
</html>
